from django.shortcuts import render, redirect
from rbac import models
from rbac.service.init_permission import init_permission


# 登录的业务逻辑处理
def login(request):
    if request.method == 'GET':
        return render(request, 'login.html')
    user = request.POST.get('user')
    pwd = request.POST.get('pwd')

    current_user = models.UserInfo.objects.filter(name=user, password=pwd).first()
    if not current_user:
        return render(request, 'login.html', {'msg': "用户名或密码错误"})
    # 权限初始化
    init_permission(request, current_user)
    return redirect('/customer/list')
